<?php

namespace App\Home\Controllers;

use Illuminate\Support\Arr;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Home\Filters\WorshipHymnFilter;
use App\Home\Models\WorshipHymn;
use App\Home\Requests\WorshipHymnRequest;
use App\Home\Resources\WorshipHymnResource;
use App\Home\Filters\WorshipCollectionFilter;
use App\Home\Models\WorshipCollection;
use App\Home\Requests\WorshipCollectionRequest;
use App\Home\Resources\WorshipCollectionResource;
use Log;

class ListeningController extends Controller
{
    public function search_hot(WorshipHymn $worshipHymn){
		$fields = [
            "name",
        ];
        $result['result'] = WorshipHymn::query()
            ->select($fields)
			->orderBy('pop', 'desc')
            ->limit(7)
			->get();
		$result['code'] = 200; 
        return $this->ok($result);
    }

    public function playlist_detail(Request $request, Int $id){
		$worshipHymn = app(WorshipHymn::class);
		$fields = [
            "*",
        ];
        $playlist = WorshipCollection::query()
            ->select($fields)
            ->find($id)
			->toArray();
		foreach ($playlist['worship_hymns'] as $key => $value) {
			$playlist['worshipHymnIds'][$key]['id'] = $value;
		}
		$playlist['worshipHymns'] = $worshipHymn->getHymnsByCollection(
			$playlist['worship_hymns']
		);
		$result['code'] = 200;
		$result['playlist'] = $playlist;
		return $this->ok($result);
    }

    public function lyric(Int $id){
		$worshipHymn = WorshipHymn::find($id);
		if (!$worshipHymn) {
			return $this->error();
		}
		$worshipHymn = $worshipHymn->load('lyric')->toArray();
		$result['code'] = 200;
		$result['lyric'] = isset($worshipHymn['lyric']['path']) ? file_get_contents($worshipHymn['lyric']['path']) : null;
		$result['nolyric'] = is_null($worshipHymn['lyric']);
		return $this->ok($result);
    }

	public function spectrum(Int $id){
		$worshipHymn = WorshipHymn::find($id);
		if (!$worshipHymn) {
			return $this->error();
		}
		$ids = json_decode($worshipHymn->spectrum);
		// 从小到大排序
		sort($ids);
		$worshipHymn->spectrum = $worshipHymn->getSpectrum($ids);
		$worshipHymn->spectrum = $worshipHymn->spectrum->toArray();
		$worshipHymn = $worshipHymn->toArray();
		$result['code'] = 200;
		$result['spectrum'] = isset($worshipHymn['spectrum']) ? $worshipHymn['spectrum'] : null;
		$result['nospectrum'] = is_null($worshipHymn['spectrum']);
		return $this->ok($result);
    }

    public function personalized(){
		$fields = [
            "id",
			"name",
			"pic_url"
        ];
		$result['result'] = WorshipCollectionResource::collection(
			WorshipCollection::query()
            ->select($fields)
            ->orderBy('sort', 'asc')
            ->get()
		);
		$result['code'] = 200;
		return $this->ok($result);
    }

    public function search(WorshipHymn $worshipHymn){		
		$result['code'] = 200;
		$result['playlist'] = $worshipHymn->getHymnsByName(request()->input('name'));
		return $this->ok($result);
    }

    public function detail(String $ids){
		$worshipHymn = app(WorshipHymn::class);
		$songs = $worshipHymn->getHymnsByCollection(
			explode(',', $ids)
		);
		$result['code'] = 200;
		$result['songs'] = $songs;
		return $this->ok($result);
    }
}
